package bank.controler;

import java.util.Iterator;
import java.util.List;

import bank.model.Customer;
import bank.view.ListView;

public class CustomerControler {

	private BankControler bankControler;
	private Customer currentCustomer;
	private ListView view = new ListView();

	public ListView getView() {
		return view;
	}

	private Customer getCustomerById(String id) {
		int customerid = Integer.parseInt(id);
		for (Iterator<Customer> i = getCustomers().iterator(); i.hasNext();) {
			Customer cust = i.next();
			if (cust.getId() == customerid)
				return cust;
		}
		return null;
	}

	public List<Customer> getCustomers() {
		return 	bankControler.getCurrentBank().getCustomers();
	}



	public Customer getCurrentCustomer() {
		return currentCustomer;
	}

	public void setCurrentCustomer(Customer currentCustomer) {
		this.currentCustomer = currentCustomer;
	}

	private void newCustomer() {
		currentCustomer = new Customer(1, "nomane", "noformane", "noaddress",
				"nozip");
		getCustomers().add(currentCustomer);
	}

	private void deleteCustomer(Customer customer) {
		for (Iterator i = getCustomers().iterator(); i.hasNext();) {
			Customer cust = (Customer) i.next();
			if (customer == cust)
				i.remove();
		}
	}

	public void add(Customer customer) {
		getCustomers().add(customer);
	}

	public String applyCustomer() {
		System.out.println("apply");
		return "edit_customer";
	}

	public String cancelCustomer() {
		System.out.println("cancel");
		return "list_customer";
	}

	public String createCustomer() {
		System.out.println("create");
		currentCustomer = null;
		newCustomer();
		return "edit_customer";
	}

	public String saveCustomer() {
		System.out.println("save");
		// saveCustomer(currentCustomer);
		return "list_customer";
	}

	public String deleteCustomer() {
		System.out.println("delete");
		deleteCustomer(currentCustomer);
		currentCustomer = getCustomers().get(0);
		return "list_customer";
	}

	public String editCustomer() {
		System.out.println("edit");
		String customerId = view.getIdCmdValue();
		System.out.println(customerId);
		currentCustomer = getCustomerById(customerId);
		return "edit_customer";
	}

	public BankControler getBankControler() {
		return bankControler;
	}

	public void setBankControler(BankControler bankControler) {
		System.out.println("*********setBankControler*********");
		this.bankControler = bankControler;
	}

	public void setView(ListView view) {
		//this.view = view;
	}

}
